package com.bigdata.booklibraryseatapi.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bigdata.booklibraryseatapi.common.exception.BusinessException;
import com.bigdata.booklibraryseatapi.common.exception.ResponseCode;
import com.bigdata.booklibraryseatapi.common.pojo.BookRecordDetail;
import com.bigdata.booklibraryseatapi.common.pojo.PageResult;
import com.bigdata.booklibraryseatapi.common.pojo.SeatStatus;
import com.bigdata.booklibraryseatapi.entity.Area;
import com.bigdata.booklibraryseatapi.entity.BookRecord;
import com.bigdata.booklibraryseatapi.entity.RecordDetail;
import com.bigdata.booklibraryseatapi.mapper.AreaMapper;
import com.bigdata.booklibraryseatapi.mapper.BookRecordMapper;
import com.bigdata.booklibraryseatapi.service.IBookRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.booklibraryseatapi.service.IRecordDetailService;
import com.bigdata.booklibraryseatapi.vo.req.*;
import com.bigdata.booklibraryseatapi.vo.resp.PageRecordRespVO;
import com.bigdata.booklibraryseatapi.vo.resp.UserBookStatusRespVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 预约记录 服务实现类
 * </p>
 *
 * @author book
 * @since 2023-12-13
 */
@Service
public class BookRecordServiceImpl extends ServiceImpl<BookRecordMapper, BookRecord> implements IBookRecordService {

    @Resource
    private BookRecordMapper bookRecordMapper;
    @Resource
    private IRecordDetailService iRecordDetailService;

    @Resource
    private AreaMapper areaMapper;

    @Transactional
    @Override
    public void createRecord(CreateRecordReqVO vo) {
        BookRecord bookRecord = new BookRecord();
        bookRecord.setUserId(vo.getUserId());
        bookRecord.setCreateTime(LocalDateTime.now());
        bookRecord.setUpdateTime(LocalDateTime.now());
        bookRecord.setStatus(1);
        int insertResult = bookRecordMapper.insert(bookRecord);
        if(insertResult != 1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
        }
        List<RecordDetail> detailList = new ArrayList<>();
        vo.getBookTimes().forEach(time -> {
            RecordDetail detail = new RecordDetail();
            detail.setRecordId(bookRecord.getId());
            detail.setAreaId(vo.getAreaId());
            detail.setDeskNum(vo.getDeskNum());
            detail.setSeatNum(vo.getSeatNum());
            detail.setBookDate(vo.getBookDate());
            detail.setBookTimeQuantumId(time);
            detailList.add(detail);
        });
        boolean saveResult = iRecordDetailService.saveBatch(detailList);
        if(!saveResult){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public void updateRecord(UpdateRecordReqVO vo) {
        BookRecord bookRecord = bookRecordMapper.selectById(vo.getId());
        bookRecord.setStatus(vo.getStatus());
        int result = bookRecordMapper.updateById(bookRecord);
        if(result != 1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public List<String> getSeatStatus(GetSeatStatusReqVO vo) {
        List<String> list = new ArrayList<>();
        SeatStatus status = new SeatStatus();
        Area area = areaMapper.selectById(vo.getAreaId());

        for (Integer bookTime : vo.getBookTimes()) {
            status.setAreaId(vo.getAreaId());
            status.setBookDate(vo.getBookDate());
            status.setBookTimeQuantumId(bookTime);
            List<BookRecordDetail> details = bookRecordMapper.selectSeatStatus(status);
            StringBuilder builder = new StringBuilder("1");
            for (int i = 0; i < area.getDeskCount(); i++) {
                builder.append("1111");
            }
            details.forEach(bookRecordDetail -> {
                int index = (bookRecordDetail.getDeskNum() - 1) * 4 + bookRecordDetail.getSeatNum();
                builder.deleteCharAt(index);
                builder.insert(index,"0");
            });
            list.add(builder.toString());
        }
        return list;
    }

    @Override
    public List<UserBookStatusRespVO> getUserBookStatus(UserBookStatusReqVO vo) {
        return bookRecordMapper.selectUserBook(vo);
    }

    @Override
    public PageResult<PageRecordRespVO> pageRecord(PageRecordReqVO vo) {
        IPage<PageRecordRespVO> result = bookRecordMapper.selectPageRecord(new Page<>(vo.getCurrent(), vo.getSize()), vo);
        return PageResult.getPage(result.getRecords(), result.getTotal());
    }
}
